package cn.wizzer.app.sys.modules.services.impl;

import cn.wizzer.app.sys.modules.models.SysPayWay;
import cn.wizzer.app.sys.modules.models.SysPayWayBank;
import cn.wizzer.app.sys.modules.models.enums.BankUsesEnum;
import cn.wizzer.app.sys.modules.models.vo.PayWayBankVO;
import cn.wizzer.app.sys.modules.models.vo.SysPayWayVO;
import cn.wizzer.app.sys.modules.services.SysPayWayBankService;
import cn.wizzer.app.sys.modules.services.SysPayWayService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.base.service.CommonDBService;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.JSONUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.OrderBy;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import java.util.List;
import java.util.stream.Collectors;


@IocBean(args = {"refer:dao"})
@Service(interfaceClass = SysPayWayService.class)
public class SysPayWayServiceImpl extends BaseServiceImpl<SysPayWay> implements SysPayWayService {
    public SysPayWayServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private CommonDBService dbService;

    @Inject
    private SysPayWayBankService bankService;

    @Override
    public Pagination listPage(Integer pageNumber, int pageSize, String searchKeyword) {
        Sql sql = Sqls.create("SELECT p.*,c.name AS currencyName,c.remark FROM sys_pay_way AS p ");
        Sql countSql = Sqls.create("SELECT COUNT(1) FROM sys_pay_way AS p ");
        String joinSql = "LEFT JOIN sys_currency AS c ON p.currencyId = c.id $condition ";
        sql.appendSourceSql(joinSql);
        countSql.appendSourceSql(joinSql);
        Cnd cnd = Cnd.NEW();
        // 关键字搜索
        if (Strings.isNotBlank(searchKeyword)) {
            searchKeyword = "%" + searchKeyword + "%";
            cnd.and(Cnd.exps("p.payWay", "like", searchKeyword)
                    .or("c.name", "like", searchKeyword));
        }
        sql.setCondition(cnd);
        sql.appendSourceSql("order by p.opAt desc");
        countSql.setCondition(cnd);
        return dbService.listPage(SysPayWayVO.class, pageNumber, pageSize, sql.toString(), countSql.toString());
    }

    @Override
    public List list(String currencyId) {
        OrderBy cnd = Cnd.where("currencyId", "=", currencyId).and("disabled", "=", false)
                .and("delFlag", "=", false).orderBy("sort", "asc");
        List<SysPayWayVO> payWays = this.query(cnd).stream().map(sysPayWay -> {
            SysPayWayVO vo = JSONUtil.toJavaBean(SysPayWayVO.class, sysPayWay);
            vo.setMaxAmount(sysPayWay.getMaxRechargeAmount());
            vo.setMinAmount(sysPayWay.getMinRechargeAmount());
            // 过滤不能充值的银行
            OrderBy condition = Cnd.where("payWayId", "=", sysPayWay.getId())
                    .and("uses", "!=", BankUsesEnum.ONLY_WITHDRAW)
                    .and("pb.disabled", "=", false)
                    .and("pb.delFlag", "=", false)
                    .asc("sort");
            List<SysPayWayBank> banks = dbService.list(PayWayBankVO.class, "SELECT pb.*,b.shortName,b.logo " +
                    "FROM sys_pay_way_bank AS pb " +
                    "LEFT JOIN sys_bank_lib AS b ON pb.bankLibId = b.id " +
                    condition);
            vo.setBanks(banks);
            return vo;
        }).collect(Collectors.toList());
        return payWays;
    }
}
